class Solution
{
public:
    string reverseOnlyLetters(string s)
    {
        vector<char> buf(s.begin(), s.end());
        int front = 0;
        int back = buf.size() - 1;
        while (true)
        {
            if (front >= back)
            {
                break;
            }
            if (!isEngCharacter(buf[front]))
            {
                ++front;
                continue;
            }
            if (!isEngCharacter(buf[back]))
            {
                --back;
                continue;
            }
            swap(buf[front], buf[back]);
            ++front;
            --back;
        }
        return string(buf.begin(), buf.end());
    }

    inline bool isEngCharacter(char c)
    {
        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    }
};